﻿using cnBlogs.Common.Helper;
using cnBlogs.DataModel;
using cnBlogs.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Grouped Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace cnBlogs.Views
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class ArticleCategory : cnBlogs.Common.LayoutAwarePage
    {
        private ArticleCategoryViewModel _categoryVM;
        public ArticleCategory()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: Assign a collection of bindable groups to this.DefaultViewModel["Groups"]
            _categoryVM = navigationParameter as ArticleCategoryViewModel;
            this.DefaultViewModel["CategoryVM"] = _categoryVM.ArticleCategoryInfoList;
            (semanticZoom.ZoomedOutView as ListViewBase).ItemsSource = this.groupedItemsViewSource.View.CollectionGroups;
        }

        private void itemGridView_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            ArticleCategoryInfo category = e.ClickedItem as ArticleCategoryInfo;
            this.Frame.Navigate(typeof(MainPage), category);

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ArticleCategoryInfo category = (sender as Button).DataContext as ArticleCategoryInfo;
            this.Frame.Navigate(typeof(MainPage), category);
        }

        private async void AddButton_Click_1(object sender, RoutedEventArgs e)
        {
            ArticleCategoryInfo item = itemGridView.SelectedItem as ArticleCategoryInfo;
            ArticleCategoryInfo had = App.DB.ArticleCategoryInfo_Table.FirstOrDefault(i => i.CategoryLink == item.CategoryLink);
            if (had == null)
            {
                App.DB.ArticleCategoryInfo_Table.Add(itemGridView.SelectedItem as ArticleCategoryInfo);
                await App.DB.SubmitChanges();
            }
            else
            {
                MessageDialog message = new MessageDialog("已添被加该分类");
                await message.ShowAsync();
            }

        }

        private void itemGridView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            ItemsChangesBottomAppBarControlHelper.BottomAppBarControl(this.itemGridView, this.bottomAppBar);

        }

    }
}
